package com.SFA.ProductShow;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.FrameWork.Common.BitmapManager;
import com.FrameWork.Entity.ImagesEntity;
import com.FrameWork.Entity.ProductEntity;

@SuppressLint("UseSparseArrays")
public class PagerImageAdapter extends PagerAdapter {

	private List<ImagesEntity> pieList;  
	private Context context; 
	private ProductEntity entity;
	private HashMap<Integer,ImageView> imageViews = new HashMap<Integer,ImageView>();



	public PagerImageAdapter(Context context, List<ImagesEntity> list, ProductEntity entity){
		this.context = context;
		changeImage(list);
		this.entity= entity;
	}

	public void changeImage(List<ImagesEntity> list){
		this.pieList = list;
		//循环生成Imageview列表
		imageViews.clear();

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return pieList.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		// TODO Auto-generated method stub
		return view == (View) obj;
	}

	@Override  
	public Object instantiateItem (ViewGroup container, int position) {  
		if(pieList==null || pieList.size()<=0)
			return null;


		ImageView iv;
		if(imageViews.containsKey(position))
		{
			iv= imageViews.get(position);
			Log.w("index", position + "");
		}
		else
		{
			Log.w("starttime", Calendar.getInstance().toString());
			iv = new ImageView(context);
			imageViews.put(position, iv);
			iv.measure(0, 0);
			try {
				ScaleType scaleType = ScaleType.CENTER;
				iv.setScaleType(scaleType);
				iv.setImageBitmap(BitmapManager.getResizedImage(pieList.get(position).getUrl(),iv.getMeasuredWidth(), iv.getMeasuredHeight()));

				iv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						/*Intent intent = new Intent(context, ProducxtDetailaActivity.class);
						intent.putExtra("id", ProductShowActivity.PHOTO_ID);
						intent.putExtra("DATA", entity);
						context.startActivity(intent);*/
					}
				});
			} catch (Exception e) {  
				e.printStackTrace();  
			}  
			Log.w("endtime", Calendar.getInstance().toString());
		}
		((ViewPager)container).addView(iv, 0);  


		return iv;  
	}  

	@Override  
	public void destroyItem (ViewGroup container, int position, Object object) {  
		container.removeView((View)object);  
	}

}
